module ChessCompare {
    //比较我选中的牌是否能打得过上家
    /*流程：
         1没选牌肯定不能出
        2选的牌不符合规则肯定不能出
        3上家没有牌我可以随意出牌的时候符合规则就能出
        4符合规则情况下打不过上家不能出
    */
    export function compare(selectChess: Array<number>): boolean {
        let canPlay = false;  //我选中的牌是否大过上家可以出牌
        //1没选牌肯定不能出
        let len = selectChess.length;
        if (len == 0) {
            return canPlay;
        }
        let chessGroup: Array<Poker_DDZ> = Poker_DDZ.sort(Poker_DDZ.idChangeObject(selectChess))//我选的牌对象,若不确定顺序必须用 Poker_DDZ.sort()排序，从手牌选出来的必定是排序过的
        let mySelectType: string = ChessType.getType(chessGroup);        //我的牌型
        //2不符合规则不出
        if (mySelectType == ChessType.cerror) {
            return canPlay;
        }
        //3上家没有牌我可以随意出牌的时候符合规则就能出
        let UpChess = DdzVO.getVo().chessListUp;
        if (UpChess.length == 0 && mySelectType != ChessType.cerror) {
            canPlay = true;
            return canPlay;
        }
        //4前面三层没过滤掉说明上家和我都有牌型。必须比大小
        let upType = DdzVO.getVo().upPlayType;//上次出的牌型
        //4-1先判断对方是火箭我无法出任何牌
        if (upType == ChessType.cboom) {
            canPlay = false;
            return canPlay;
        }
        //4-2我是火箭必可出
        if (mySelectType == ChessType.cboom) {
            canPlay = true;
            return canPlay;
        }

        //4-3然后按照对方的牌型比较我的牌型是否可出
        if (upType == ChessType.c1 && mySelectType == ChessType.c1)
            canPlay = isDan(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c2 && mySelectType == ChessType.c2)
            canPlay = isDuiZi(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c3 && mySelectType == ChessType.c3)
            canPlay = isSanBuDai(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c4 && mySelectType == ChessType.c4)
            canPlay = isZhaDan(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c31 && mySelectType == ChessType.c31)
            canPlay = isSanDaiYi(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c123 && mySelectType == ChessType.c123)
            canPlay = isShunZi(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c1122 && mySelectType == ChessType.c1122)
            canPlay = isLianDui(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c32 && mySelectType == ChessType.c32)
            canPlay = isSanDaiEr(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c111222 && mySelectType == ChessType.c111222)
            canPlay = isDuoFeiJiBuDai(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c11122234 && mySelectType == ChessType.c11122234)
            canPlay = isDuoFeiJiDaiDan(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c1112223344 && mySelectType == ChessType.c1112223344)
            canPlay = isDuoFeiJiDaiShuang(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c411 && mySelectType == ChessType.c411)
            canPlay = isSiDaiEr(UpChess, chessGroup, canPlay)
        else if (upType == ChessType.c422 && mySelectType == ChessType.c422)
            canPlay = isSiDaiErDui(UpChess, chessGroup, canPlay)

        //4-4最后看我是否有炸弹（对方不炸的情况下，对方若出炸，4-3已处理
        if (upType != ChessType.c4 && mySelectType == ChessType.c4) {
            canPlay = true;
            return canPlay;
        }
        return canPlay;
    }

    export function isDan(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[0].value > UpChess[0].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isDuiZi(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[0].value > UpChess[0].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isSanBuDai(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[0].value > UpChess[0].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isZhaDan(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[0].value > UpChess[0].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isSanDaiYi(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[1].value > UpChess[1].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isShunZi(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (UpChess.length == chessGroup.length) {
            if (chessGroup[0].value > UpChess[0].value) {
                canPlay = true;
            }
        }
        return canPlay;
    }

    export function isLianDui(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (UpChess.length == chessGroup.length) {
            if (chessGroup[0].value > UpChess[0].value) {
                canPlay = true;
            }
        }
        return canPlay;
    }

    export function isSanDaiEr(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[2].value > UpChess[2].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isSiDaiEr(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (chessGroup[2].value > UpChess[2].value) {
            canPlay = true;
        }
        return canPlay;
    }

    export function isSiDaiErDui(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        let list = chessSplice(UpChess);
        let list2 = chessSplice(chessGroup);
        let upNum1 = list[3][0][0].value;
        let upNum2 = list2[3][0][0].value;
        if (upNum2 > upNum1) {
            canPlay = true;
        }
        return canPlay;
    }
    export function isDuoFeiJiBuDai(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (UpChess.length == chessGroup.length) {
            if (chessGroup[0].value > UpChess[0].value) {
                canPlay = true;
            }
        }
        return canPlay;
    }

    export function isDuoFeiJiDaiDan(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (UpChess.length == chessGroup.length) {
            let list = chessSplice(UpChess);
            let list2 = chessSplice(chessGroup);
            let upNum1 = list[2][0][0].value;
            let upNum2 = list2[2][0][0].value;
            if (upNum2 > upNum1) {
                canPlay = true;
            }
        }
        return canPlay;
    }

    export function isDuoFeiJiDaiShuang(UpChess: Array<Poker_DDZ>, chessGroup: Array<Poker_DDZ>, canPlay: boolean): boolean {
        if (UpChess.length == chessGroup.length) {
            let list = chessSplice(UpChess);
            let list2 = chessSplice(chessGroup);
            let upNum1 = list[2][0][0].value;
            let upNum2 = list2[2][0][0].value;
            if (upNum2 > upNum1) {
                canPlay = true;
            }
        }
        return canPlay;
    }
    //把手牌按出现次数拆分并装入一个数组。数组下标对应：0 单 1 对子 2 飞机 3 炸弹
    export function chessSplice(myChess: Array<Poker_DDZ>): Array<Array<Array<Poker_DDZ>>> {
        //先看每张牌出现次数
        let list = [];
        for (let i = 0; i < myChess.length; i++) {
            if (Global.isEmpty(list[myChess[i].value])) {
                list[myChess[i].value] = [];
            }
            list[myChess[i].value].push(myChess[i]);
        }
        //按出现次数装入数组，下标 0 单 2 对 3 飞机 4 炸弹
        let list2: Array<Array<Array<Poker_DDZ>>> = [[], [], [], []]
        for (let i in list) {
            if (Global.isEmpty(list[i])) {
                continue;
            }
            list2[list[i].length - 1].push(list[i]);
        }
        return list2;
    }
} 